import { Injectable } from '@angular/core';
import { environment } from 'src/environments/environment';
import { HttpClient, HttpParams } from '@angular/common/http';
@Injectable({
  providedIn: 'root'
})
export class SalesService {

  data = '/Sale'
  constructor(private http: HttpClient) { }
  GetSalesQuotations(data:{
    PartNum?:string,
    PartName?:string,
    CustomerName?:string,
    StartTime?:string,
    EndTime?:string,
    QuotationNum?:string,
    PageNumber?:number,
    PageSize?:number
  }){
    const GetSalesQuotations = '/GetSalesQuotations';
    return this.http.get(environment.apiUrl+ this.data+GetSalesQuotations,{params:data});
  }// 获取销售报价单
  CreateSalesQuotation(data:any){
    const CreateSalesQuotation = '/CreateSalesQuotation';
    return this.http.post(environment.apiUrl+ this.data+CreateSalesQuotation,data);
  }// 创建销售报价单
  ApprovalSalesQuotation(salesQuotationId:any,isApproved:any){
    const ApprovalSalesQuotation = '/ApprovalSalesQuotation';
    return this.http.put(environment.apiUrl+ this.data+ApprovalSalesQuotation,null,{params:{salesQuotationId,isApproved}});
  }// 审批销售报价单
  UnApprovalSalesQuotation(salesQuotationId:any){
    const UnApprovalSalesQuotation = '/UnApprovalSalesQuotation';
    return this.http.put(environment.apiUrl+ this.data+UnApprovalSalesQuotation,null,{params:{salesQuotationId}});
  }// 反审批销售报价单
  UpdateSalesQuotation(salesQuotationId:any,data:any){
    const UpdateSalesQuotation = '/UpdateSalesQuotation';
    return this.http.put(environment.apiUrl+ this.data+UpdateSalesQuotation,data,{params:{salesQuotationId}});
  }// 修改销售报价单

  GetSalesQuotationById(id:any){
    const GetSalesQuotationById = '/GetSalesQuotationById';
    return this.http.get(environment.apiUrl+ this.data+GetSalesQuotationById,{params:{id}});
  }// 根据ID获取销售报价单
  DeleteSalesQuotations(salesQuotationIds:any){
    const DeleteSalesQuotations = '/DeleteSalesQuotations';
    const options = {
      body: salesQuotationIds,
      responseType: 'text' as 'json'
    };
    return this.http.delete(environment.apiUrl+ this.data+DeleteSalesQuotations,options);
  }// 删除销售报价单
  FinishSalesQuotation(salesQuotationId:any){
    const FinishSalesQuotation = '/FinishSalesQuotation';
    return this.http.put(environment.apiUrl+ this.data+FinishSalesQuotation,null,{params:{salesQuotationId}});
  }// 完成销售报价单
  UnFinishSalesQuotation(salesQuotationId:any){
    const UnFinishSalesQuotation = '/UnFinishSalesQuotation';
    return this.http.put(environment.apiUrl+ this.data+UnFinishSalesQuotation,null,{params:{salesQuotationId}});
  }// 取消完成销售报价单
  GetLogsBySalesQuotationId(data:{
    Id:any,
    PageNumber:number,
    PageSize:number
  }){
    const GetLogsBySalesQuotationId = '/GetLogsBySalesQuotationId';
    return this.http.get(environment.apiUrl+ this.data+GetLogsBySalesQuotationId,{params:data});
  }// 根据销售报价单ID获取日志
  GetAllSalesQuotationLogs(data:{
    PageNumber:number,
    PageSize:number
  }){
    const GetAllSalesQuotationLogs = '/GetAllSalesQuotationLogs';
    return this.http.get(environment.apiUrl+ this.data+GetAllSalesQuotationLogs,{params:data});
  }// 获取所有销售报价单日志

  GetSaleOrders(data:{
    PartNum?:string,
    PartName?:string,
    CustomerName?:string,
    CustomerOrderNum?:string,
    CustomerShortName?:string,
    StartTime?:string,
    EndTime?:string,
    QuotationNum?:string,
    SaleOrderNum?:string,
    PageNumber?:number,
    PageSize?:number
  }){
    const GetSaleOrders = '/GetSaleOrders';
    return this.http.get(environment.apiUrl+ this.data+GetSaleOrders,{params:data});
  }
  CreateSaleOrder(data:any){
    const CreateSaleOrder = '/CreateSaleOrder';
    return this.http.post(environment.apiUrl+ this.data+CreateSaleOrder,data);
  }
  DeleteSaleOrders(saleOrderIds:any){
    const DeleteSaleOrders = '/DeleteSaleOrders';
    const options = {
      body: saleOrderIds,
      responseType: 'text' as 'json'
    };
    return this.http.delete(environment.apiUrl+ this.data+DeleteSaleOrders,options);
  }
  ApprovalSaleOrder(saleOrderId:any){
    const ApprovalSaleOrder = '/ApprovalSaleOrder';
    return this.http.put(environment.apiUrl+ this.data+ApprovalSaleOrder,null,{params:{saleOrderId}});
  }
  UnApprovalSaleOrder(saleOrderId:any){
    const UnApprovalSaleOrder = '/UnApprovalSaleOrder';
    return this.http.put(environment.apiUrl+ this.data+UnApprovalSaleOrder,null,{params:{saleOrderId}});
  }
  UpdateSaleOrder(saleOrderId:any,data:any){
    const UpdateSaleOrder = '/UpdateSaleOrder';
    return this.http.put(environment.apiUrl+ this.data+UpdateSaleOrder,data,{params:{saleOrderId}});
  }
  GetSaleOrderById(id:any){
    const GetSaleOrderById = '/GetSaleOrderById';
    return this.http.get(environment.apiUrl+ this.data+GetSaleOrderById,{params:{id}});
  }
  FinishSaleOrder(saleOrderId:any){
    const FinishSaleOrder = '/FinishSaleOrder';
    return this.http.put(environment.apiUrl+ this.data+FinishSaleOrder,null,{params:{saleOrderId}});
  }
  UnFinishSaleOrder(saleOrderId:any){
    const UnFinishSaleOrder = '/UnFinishSaleOrder';
    return this.http.put(environment.apiUrl+ this.data+UnFinishSaleOrder,null,{params:{saleOrderId}});
  }
  GetLogsBySaleOrderId(data:{
    Id:any,
    PageNumber:number,
    PageSize:number
  }){
    const GetLogsBySaleOrderId = '/GetLogsBySaleOrderId';
    return this.http.get(environment.apiUrl+ this.data+GetLogsBySaleOrderId,{params:data});
  }
  GetAllSaleOrderLogs(data:{
    PageNumber:number,
    PageSize:number
  }){
    const GetAllSaleOrderLogs = '/GetAllSaleOrderLogs';
    return this.http.get(environment.apiUrl+ this.data+GetAllSaleOrderLogs,{params:data});
  }
  GetQuotationItems(data:{
    ZhuanHuanWanDe:boolean,
    PartNum?:string,
    PartName?:string,
    CustomerName?:string,
    StartTime?:string,
    EndTime?:string,
    QuotationNum?:string,
    PageNumber?:number,
    PageSize?:number
  }){
    const GetQuotationItems = '/GetQuotationItems';
    return this.http.get(environment.apiUrl+ this.data+GetQuotationItems,{params:data});
  }
  GetSalesQuotationByIds(ids: any[]) {
    const GetSalesQuotationByIds = '/GetSalesQuotationByIds';
    let params = new HttpParams();
    ids.forEach(id => {
      params = params.append('ids', id);
    });
    return this.http.get(`${environment.apiUrl}${this.data}${GetSalesQuotationByIds}`, { params: params });
  }
  GetQuotationItemsWithQuotation(data:{
    ZhuanHuanWanDe:boolean,
    PartNum?:string,
    PartName?:string,
    CustomerName?:string,
    StartTime?:string,
    EndTime?:string,
    QuotationNum?:string,
    PageNumber?:number,
    PageSize?:number
  }){
    const GetQuotationItemsWithQuotation = '/GetQuotationItemsWithQuotation';
    return this.http.get(environment.apiUrl+ this.data+GetQuotationItemsWithQuotation,{params:data});
  }
  GetSaleOrderPartsBySaleOrderIds(data: {
    ZhuanHuanWanGongDanDe: boolean,
    ZhuanHuanWanDe: boolean,
    ids: any[],
    PartNum?: string,
    PartName?: string,
    CustomerName?: string,
    StartTime?: string,
    EndTime?: string,
    SaleOrderNum?: string,
    PageNumber?: number,
    PageSize?: number
  }) {
    const GetSaleOrderPartsBySaleOrderIds = '/GetSaleOrderPartsBySaleOrderIds';
    let params = new HttpParams();

    // Append array parameters
    data.ids.forEach(id => {
      params = params.append('ids', id);
    });

    // Append other parameters if they exist
    if (data.PartNum) {
      params = params.append('PartNum', data.PartNum);
    }
    if (data.PartName) {
      params = params.append('PartName', data.PartName);
    }
    if (data.CustomerName) {
      params = params.append('CustomerName', data.CustomerName);
    }
    if (data.StartTime) {
      params = params.append('StartTime', data.StartTime);
    }
    if (data.EndTime) {
      params = params.append('EndTime', data.EndTime);
    }
    if (data.SaleOrderNum) {
      params = params.append('SaleOrderNum', data.SaleOrderNum);
    }
    if (data.PageNumber !== undefined) {
      params = params.append('PageNumber', data.PageNumber.toString());
    }
    if (data.PageSize !== undefined) {
      params = params.append('PageSize', data.PageSize.toString());
    }
    params = params.append('ZhuanHuanWanGongDanDe', data.ZhuanHuanWanGongDanDe.toString());
    params = params.append('ZhuanHuanWanDe', data.ZhuanHuanWanDe.toString());

    return this.http.get(`${environment.apiUrl}${this.data}${GetSaleOrderPartsBySaleOrderIds}`, { params: params });
  }
  GetSaleOrdersBySaleOrderIds(ids: any[],ZhuanHuanWanGongDanDe:boolean,ZhuanHuanWanDe:boolean, pageNumber: number, pageSize: number) {
    const GetSaleOrdersBySaleOrderIds = '/GetSaleOrdersBySaleOrderIds';
    let params = new HttpParams();
    // 将 ids 数组中的每个 id 作为单独的参数追加
    ids.forEach(id => {
      params = params.append('ids', id);
    });
    // 设置 ZhuanHuanWanGongDanDe 和 ZhuanHuanWanDe 参数
    params = params.append('ZhuanHuanWanGongDanDe', ZhuanHuanWanGongDanDe.toString());
    params = params.append('ZhuanHuanWanDe', ZhuanHuanWanDe.toString());
    // 设置 pageNumber 和 pageSize 参数
    params = params.append('pageNumber', pageNumber.toString());
    params = params.append('pageSize', pageSize.toString());
    return this.http.get(`${environment.apiUrl}${this.data}${GetSaleOrdersBySaleOrderIds}`, { params: params });
  }
  GetSaleOrderPartsWithSaleOrder(data:{
    ZhuanHuanWanGongDanDe:boolean,
    ZhuanHuanWanDe:boolean,
    PartNum?:string,
    PartName?:string,
    CustomerName?:string,
    StartTime?:string,
    EndTime?:string,
    SaleOrderNum?:string,
    PageNumber?:number,
    PageSize?:number
  }){
    const GetSaleOrderPartsWithSaleOrder = '/GetSaleOrderPartsWithSaleOrder';
    return this.http.get(environment.apiUrl+ this.data+GetSaleOrderPartsWithSaleOrder,{params:data});
  }
  GetPagedSaleDeliver(data:{
    SearchTermPart?:string,
    SearchTermCustomer?:string,
    SearchStatus?:string,
    SaleOrderNum?:string,
    SaleDeliverNum?:string,
    OrderDateStartTime?:string,
    OrderDateEndTime?:string,
    CreateTimeStartTime?:string,
    CreateTimeEndTime?:string,
    PageNumber:number,
    PageSize:number
  }){
    const GetPagedSaleDeliver = '/GetPagedSaleDeliver';
    return this.http.get(environment.apiUrl+ this.data+GetPagedSaleDeliver,{params:data});
  } // 获取销售发货单
   CreateSaleDeliver(data:any){
    const CreateSaleDeliver = '/CreateSaleDeliver';
    return this.http.post(environment.apiUrl+ this.data+CreateSaleDeliver,data);
  } // 创建销售发货单
  GetSaleDeliverById(id:any){
    const GetSaleDeliverById = '/GetSaleDeliverById';
    return this.http.get(environment.apiUrl+ this.data+GetSaleDeliverById,{params:{id}});
  }
  UpdateSaleDeliver(id:any,data:any){
    const UpdateSaleDeliver = '/UpdateSaleDeliver';
    return this.http.put(environment.apiUrl+ this.data+UpdateSaleDeliver,data,{params:{id}});
  }
  DeleteSaleDeliver(id:any){
    const DeleteSaleDeliver = '/DeleteSaleDeliver';
    return this.http.put(environment.apiUrl+ this.data+DeleteSaleDeliver,null,{params:{id}});
  }
  FinishSaleDeliver(id:any){
    const FinishSaleDeliver = '/SaleDeliver';
    return this.http.post(environment.apiUrl+ this.data+FinishSaleDeliver,null,{params:{id}});
  }
  AuditSaleDeliver(id:any){
    const AuditSaleDeliver = '/AuditSaleDeliver';
    return this.http.put(environment.apiUrl+ this.data+AuditSaleDeliver,null,{params:{id}});
  }
  UnAuditSaleDeliver(id:any){
    const UnAuditSaleDeliver = '/UnAuditSaleDeliver';
    return this.http.put(environment.apiUrl+ this.data+UnAuditSaleDeliver,null,{params:{id}});
  }
  GetPagedSaleReturn(data:{
    SearchTermPart?:string,
    SearchTermCustomer?:string,
    SearchStatus?:string,
    SaleOrderNum?:string,
    SaleReturnNum?:string,
    SaleDeliverNum?:string,
    CustomerId?:string,
    OrderDateStartTime?:string,
    OrderDateEndTime?:string,
    CreateTimeStartTime?:string,
    CreateTimeEndTime?:string,
    PageNumber:number,
    PageSize:number
  }){
    const GetPagedSaleReturn = '/GetPagedSaleReturn';
    return this.http.get(environment.apiUrl+ this.data+GetPagedSaleReturn,{params:data});
  }
  GetSaleReturnById(id:any){
    const GetSaleReturnById = '/GetSaleReturnById';
    return this.http.get(environment.apiUrl+ this.data+GetSaleReturnById,{params:{id}});
  }
  CreateSaleReturn(data:any){
    const CreateSaleReturn = '/CreateSaleReturn';
    return this.http.post(environment.apiUrl+ this.data+CreateSaleReturn,data);
  }
  UpdateSaleReturn(id:any,data:any){
    const UpdateSaleReturn = '/UpdateSaleReturn';
    return this.http.put(environment.apiUrl+ this.data+UpdateSaleReturn,data,{params:{id}});
  }
  UploadAttachment(file:File){
    const url = this.data+'/UploadAttachment';
    const formData = new FormData();
    formData.append('file', file);
    return this.http.post(environment.apiUrl+ url,formData);
  }
  UploadAvatar(file:File){
    const url = this.data+'/UploadAvatar';
    const formData = new FormData();
    formData.append('file', file);
    return this.http.post(environment.apiUrl+ url,formData);
  }
  GetSaleReturnPartsBySaleReturnIds(ids: any[]){
    const GetSaleReturnPartsBySaleReturnIds = '/GetSaleReturnPartsBySaleReturnIds';
    let params = new HttpParams();
    ids.forEach(id => {
      params = params.append('ids', id);
    });
    return this.http.get(`${environment.apiUrl}${this.data}${GetSaleReturnPartsBySaleReturnIds}`, { params: params });
  }
  DownloadSaleQuotationFileModel(){
    const url = '/Excel/DownloadSaleQuotationFileModel';
    return this.http.get(environment.apiUrl+ url,{responseType: 'blob'});
  }// 下载销售报价单模板
  DownloadSaleQuotationFile(){
    const url = '/Excel/DownloadSaleQuotationFile';
    return this.http.get(environment.apiUrl+ url,{responseType: 'blob'});
  }// 下载销售报价单
  ImportSaleQuotationFromExcel(file:File){
    const formData: FormData = new FormData();
    formData.append('file', file, file.name);
    const url = '/ImportSaleQuotationFromExcel';
    return this.http.post(environment.apiUrl + '/Excel' + url, formData, { responseType: 'blob'
    });
  }// 导入销售报价单
  DownloadSaleOrderFileModel(){
    const url = '/Excel/DownloadSaleOrderFileModel';
    return this.http.get(environment.apiUrl+ url,{responseType: 'blob'});
  }// 下载销售订单模板
  DownloadSaleOrderFile(){
    const url = '/Excel/DownloadSaleOrderFile';
    return this.http.get(environment.apiUrl+ url,{responseType: 'blob'});
  }// 下载销售订单
  ImportSaleOrderFromExcel(file:File){
    const formData: FormData = new FormData();
    formData.append('file', file, file.name);
    const url = '/ImportSaleOrderFromExcel';
    return this.http.post(environment.apiUrl + '/Excel' + url, formData, { responseType: 'blob'
    });
  }// 导入销售订单
  GetSaleDeliverPart(data:{
    PageSize:number,
    PageNumber:number,
    SearchTermPart?:string,
    SearchTermCustomer?:string,
    SaleDeliverNum?:string,
    CustomerId?:string,
    State?:string,
    ZhuanHuanWanDe?:boolean
  }){
    const GetSaleDeliverPart = '/GetSaleDeliverPart';
    return this.http.get(environment.apiUrl+ this.data+GetSaleDeliverPart,{params:data});
  }
  SaleReturn(id:any){
    const url = '/SaleReturn';
    return this.http.post(environment.apiUrl+ this.data+url,null,{params:{id}});
  }
  ApprovalSaleReturn(saleReturnId:any){
    const url = '/ApprovalSaleReturn';
    return this.http.put(environment.apiUrl+ this.data+url,null,{params:{saleReturnId}});
  }
  UnApprovalSaleReturn(saleReturnId:any){
    const url = '/UnApprovalSaleReturn';
    return this.http.put(environment.apiUrl+ this.data+url,null,{params:{saleReturnId}});
  }
  DeleteSaleReturn(id:any){
    const url = '/DeleteSaleReturn';
    return this.http.put(environment.apiUrl+ this.data+url,null,{params:{id}});
  }
  uploadSaleOrderPartsFromExcel(file: File) {
    const formData = new FormData();
    formData.append('file', file, file.name);
    const url = '/GetQuotationItemsFromExcel';
    return this.http.post(environment.apiUrl + this.data + url, formData);
  }
  GetSaleOrderPartsFromExcel(file:File){
    const formData: FormData = new FormData();
    formData.append('file', file, file.name);
    const url = '/GetSaleOrderPartsFromExcel';
    return this.http.post(environment.apiUrl+ this.data+url,formData);
  }
  GetLogsBySaleDeliverId(data:{
    Id:any,
    PageNumber:number,
    PageSize:number
  }){
    const url = '/GetLogsBySaleDeliverId';
    return this.http.get(environment.apiUrl+ this.data+url,{params:data});
  }
  GetAllSaleDeliverLogs(data:{
    PageNumber:number,
    PageSize:number
  }){
    const url = '/GetAllSaleDeliverLogs';
    return this.http.get(environment.apiUrl+ this.data+url,{params:data});
  }
  GetLogsBySaleReturnId(data:{
    Id:any,
    PageNumber:number,
    PageSize:number
  }){
    const url = '/GetLogsBySaleReturnId';
    return this.http.get(environment.apiUrl+ this.data+url,{params:data});
  }
  GetAllSaleReturnLogs(data:{
    PageNumber:number,
    PageSize:number
  }){
    const url = '/GetAllSaleReturnLogs';
    return this.http.get(environment.apiUrl+ this.data+url,{params:data});
  }
  CloseQuotationItem(data:any,state:boolean){
    return this.http.put(environment.apiUrl+this.data+'/CloseQuotationItem',data,{params:{close:state}})
  }
  CloseSaleOrderParts(data:any,state:boolean){
    return this.http.put(environment.apiUrl+this.data+'/CloseSaleOrderParts',data,{params:{close:state}})
  }

}
